کانال بله, جهت پشتیبانی و اطلاع رسانی کانال بله, جهت پشتیبانی و اطلاع رسانی
عضویت

آموزش SI with Map 2-Java Spring

SI توسط map (2)

مثال تزریق setter با استفاده از map غیر رشته ای (شامل شی وابسته)

در این مثال از map به عنوان پاسخی استفاده می شود که شامل پاسخ و کاربر است. در اینجا جفت کلید و مقدار به عنوان شی در نظر گرفته می شود. پاسخ و کاربر هر کدام اطلاعات منحصر به خود را دارند که به ترتیب عبارتند از [answerId,answer,postedDate] و [userId,username,emailId] .

همانند مثال های قبلی، این مثال نیز برگرفته از فرومی است که" یک سوال می تواند چندین پاسخ داشته باشد" .


Question.java:

این کلاس شامل سه خصیصه، getter وsetter ها و متد displayInfo() برای نمایش اطلاعات است.

package com.javatpoint;  
import java.util.Iterator;  
import java.util.Map;  
import java.util.Set;  
import java.util.Map.Entry;  
public class Question {  
private int id;  
private String name;  
private Map< Answer,User> answers;  
//getters and setters    
public void displayInfo(){  
    System.out.println("question id:"+id);  
    System.out.println("question name:"+name);  
    System.out.println("Answers....");  
    Set< Entry< Answer, User>> set=answers.entrySet();
    Iterator< Entry< Answer, User>> itr=set.iterator();
    while(itr.hasNext()){
        Entry< Answer, User> entry=itr.next();
        Answer ans=entry.getKey();
        User user=entry.getValue();
        System.out.println("Answer Information:");
        System.out.println(ans);
        System.out.println("Posted By:");
        System.out.println(user);
    }
}
}

Answer.java:

package com.javatpoint;  
  
import java.util.Date;  
  
public class Answer {  
private int id;  
private String answer;  
private Date postedDate;  
public Answer() {}  
public Answer(int id, String answer, Date postedDate) {  
    super();  
    this.id = id;  
    this.answer = answer;  
    this.postedDate = postedDate;  
}  
  
public String toString(){  
    return "Id:"+id+" Answer:"+answer+" Posted Date:"+postedDate;  
}  
}       

User.java:

package com.javatpoint;  
  
public class User {  
private int id;  
private String name,email;  
public User() {}  
public User(int id, String name, String email) {  
    super();  
    this.id = id;  
    this.name = name;  
    this.email = email;  
}  
  
public String toString(){  
    return "Id:"+id+" Name:"+name+" Email Id:"+email;  
}  
}       

applicationContext.xml:

برای تعریف مرجع bean در map از دو خصیصه key-ref و value-ref مربوط به عنصر entry استفاده می شود.

< ?xml version="1.0" encoding="UTF-8" ?>

< beans 3. xmlns="http://www.springframework.org/schema/beans"
         4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         5. xmlns:p="http://www.springframework.org/schema/p"
         6. xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  
< bean id="answer1" class="com.javatpoint.Answer">  
< property name="id" value="1">< /property>  
< property name="answer" value="Java is a Programming Language">< /property>  
< property name="postedDate" value="12/12/2001">< /property>  
< /bean>  
< bean id="answer2" class="com.javatpoint.Answer">  
< property name="id" value="2">< /property>  
< property name="answer" value="Java is a Platform">< /property>  
< property name="postedDate" value="12/12/2003">< /property>  
< /bean>  
  
< bean id="user1" class="com.javatpoint.User">  
< property name="id" value="1">< /property>  
< property name="name" value="Arun Kumar">< /property>  
< property name="email" value="arun@gmail.com">< /property>  
< /bean>  
< bean id="user2" class="com.javatpoint.User">  
< property name="id" value="2">< /property>  
< property name="name" value="Varun Kumar">< /property>  
< property name="email" value="Varun@gmail.com">< /property>  
< /bean>  
  
< bean id="q" class="com.javatpoint.Question">  
< property name="id" value="1">< /property>  
< property name="name" value="What is Java?">< /property>  
< property name="answers">  
< map>  
< entry key-ref="answer1" value-ref="user1">< /entry>  
< entry key-ref="answer2" value-ref="user2">< /entry>  
< /map>  
< /property>  
< /bean>  
< /beans>    

Test.java:

این کلاس bean را از فایل applicationContext.xml می گیرد و متد displayInfo() را برای نمایش اطلاعات فراخوانی می کند.

package com.javatpoint;  
  
import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.core.io.Resource;  
  
public class Test {  
public static void main(String[] args) {  
    Resource r=new ClassPathResource("applicationContext.xml");  
    BeanFactory factory=new XmlBeanFactory(r);  
      
    Question q=(Question)factory.getBean("q");  
    q.displayInfo();  
      
}  
}        
1398/12/14 1666 583
رمز عبور : tahlildadeh.com یا www.tahlildadeh.com
نظرات شما

نظرات خود را ثبت کنید...